package com.xca.znb.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.xca.znb.R;

public class Welcome extends AppCompatActivity {
    private CountDownTimer countDownTimer ;
    private long timeLeftInMillis = 4000 ;
    private TextView times ;
    private ImageButton finishThis ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        times = findViewById(R.id.times);
        finishThis = findViewById(R.id.welcome_no);

        startCountdown();

        finishThis.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intentActivity();
                finish();
            }
        });


    }

    private void startCountdown() {
        countDownTimer = new CountDownTimer(timeLeftInMillis , 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeLeftInMillis = millisUntilFinished ;
                int secondsRemaining = (int) (millisUntilFinished/1000) ;
                times.setText(secondsRemaining + "s");
            }

            @Override
            public void onFinish() {
                intentActivity();
                finish();
            }
        }.start() ;
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        if (countDownTimer != null){
            countDownTimer.cancel();
        }
    }

    protected void onDestroyBtn(){
        super.onDestroy();
        if (countDownTimer != null){
            countDownTimer.cancel();
        }
    }
    private void intentActivity(){
        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone" , "");//收藏user
        if (phone.isEmpty()){
            Intent intent = new Intent(Welcome.this , LoginActivity.class);
            startActivity(intent);
        }else {
            Intent intent = new Intent(Welcome.this , Main.class);
            startActivity(intent);
        }
    }
}